package com.hxd.otc.module.convenience.presenter;

import android.content.res.AssetManager;
import android.support.annotation.NonNull;

import com.google.gson.reflect.TypeToken;
import com.hxd.otc.base.BaseBean;
import com.hxd.otc.module.convenience.bean.ChooseHydropowerCityBean;
import com.hxd.otc.module.convenience.bean.JsonBean;
import com.hxd.otc.module.convenience.contract.ChooseHydropwerCityContract;
import com.hxd.otc.module.convenience.model.ConvenienceModel;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.net.BaseObserver;
import com.hxd.otc.utils.DataUtils;
import com.hxd.otc.utils.GsonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 包  名 :  com.example.administrator.blocksbankclient.mvp.order
 * 类  名 :  OrderPresenter
 * 作  者 :  zjl
 * 时  间 :  2018/6/4 17:02
 * 描  述 :  ${TODO}所有订单页面P
 */
public class ChooseHydropwerCityPresenter extends BasePresenter<ChooseHydropwerCityContract.View> implements ChooseHydropwerCityContract.Presenter {

    ConvenienceModel model;
    private ArrayList<JsonBean> options1Items = new ArrayList<>();
    private ArrayList<ArrayList<String>> options2Items = new ArrayList<>();

    public ChooseHydropwerCityPresenter(ChooseHydropwerCityContract.View view) {
        super(view);
        model = ConvenienceModel.getInstance();
    }

    @Override
    public void start() {
    }


    @Override
    public void doShowNoMore() {
        mView.onShowNoMore();
    }

    @Override
    public void doSetAdapter(List<ChooseHydropowerCityBean> list) {
        mView.onSetAdapter(list);
    }


    @Override
    public void getLocation(String province, String city, String rechargeType) {

        model.getLocation(province, city, rechargeType, new BaseObserver<ArrayList<ChooseHydropowerCityBean>>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull ArrayList<ChooseHydropowerCityBean> beans) {
                doSetAdapter(beans);
                doShowNoMore();
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {
                mView.showToast(errMessage);
                doShowNoMore();
            }
        }, mView.<BaseBean<ArrayList<ChooseHydropowerCityBean>>>bindAutoDispose());
    }

    @Override
    public void doGetProvince(@NonNull AssetManager assets, @NonNull String path) {
        DataUtils.readAssets(assets, path, new DataUtils.OnReaderBufferResult() {
            @Override
            public void onResult(String json) {
                ArrayList<JsonBean> jsonBean = GsonUtil.fromJson(json, new TypeToken<List<JsonBean>>() {
                }.getType());
                options1Items = jsonBean;
                for (int i = 0; i < jsonBean.size(); i++) {
                    ArrayList<String> CityList = new ArrayList<>();//该省的城市列表（第二级）
                    for (int j = 0; j < jsonBean.get(i).getCityList().size(); j++) {
                        String CityName = jsonBean.get(i).getCityList().get(j).getName();
                        CityList.add(CityName);//添加城市
                    }
                    options2Items.add(CityList);
                }
            }
        });
    }

    public ArrayList<JsonBean> getOptions1Items() {
        return options1Items;
    }

    public ArrayList<ArrayList<String>> getOptions2Items() {
        return options2Items;
    }
}
